package com.gdpu.project.util.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLResult;
import org.dom4j.io.XMLWriter;

import com.sun.xml.internal.ws.streaming.XMLReader;

public class Pie3DXML {
	public static Boolean createXML(Map<String, String> map, String fileName,String captionAndyAxisName,String xAxisName) {
		Boolean flag = false;
		try {
			/** 定义创建时间 */
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String date = s.format(new Date());

			/** 建立document对象 */
			Document document = DocumentHelper.createDocument();

			/** 建立XML文档的根graph */
			Element graphElement = document.addElement("graph");

			/** 加入一行注释 */
			graphElement.addComment("This is a new XML file,创建日期：" + date);

			/** 创建根节点属性 */
			graphElement.addAttribute("showNames", "1");
			graphElement.addAttribute("decimalPrecision", "0");
//			graphElement.addAttribute("showPercentageValues", "1");
			graphElement.addAttribute("showPercentageInLabel", "1");
			graphElement.addAttribute("pieRadius", "150");
			graphElement.addAttribute("pieSliceDepth", "20");
			String yAxisName="CgProject".equals(captionAndyAxisName)?"kysmNumber":"CgThesis".equals(captionAndyAxisName)?"lwNumber":"CgConference"
				.equals(captionAndyAxisName)?"xshyNumber":"CgInnovate".equals(captionAndyAxisName)?"zdxskjcxNumber":"CgPatent".equals(captionAndyAxisName)?
							"zlNumber":"zzNumber";
//			graphElement.addAttribute("yAxisName", yAxisName);
			String xAxisName1="department".equals(xAxisName)?"教研室名":"postion".equals(xAxisName)?"职称名":"education"
				.equals(xAxisName)?"学历名":"";
			graphElement.addAttribute("xAxisName", xAxisName1);
//			graphElement.addAttribute("canvasBgDepth", "5");
//			graphElement.addAttribute("rotateNames", "1");
			

			/** 创建set节点 */
			Set<String> keys = map.keySet();
			Iterator<String> it = keys.iterator();
			while (it.hasNext()) {
				String name = it.next();
				String value = map.get(name);
				Element setElement = graphElement.addElement("set");
				setElement.addAttribute("name", name);
				setElement.addAttribute("value", value);
			}

			/** 将document中的内容写入文件中 */
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");
			XMLWriter writer = new XMLWriter(new OutputStreamWriter(
					new FileOutputStream(fileName), "UTF-8"));
			writer.write(document);
			writer.close();
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("生成出错啦！");
		}
		return flag;
	}
	public static String readXML(String srcURL) throws Exception{
		Reader reader=new FileReader(new File(srcURL));
		String value="";
		int temp;
		while((temp=reader.read())!=-1){
			value+=(char)temp;
		}
		value=value.replace("\n", "").replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
		return value;
	}
	

}
